# -*- coding: utf-8 -*-
# @Time    : 2025/1/2 11:18
# @Author  : sjh
# @Site    :
# @File    : decode_video.py
# @Comment :
import os
from datetime import datetime

from orbbec.depth_rgb_pipeline import get_frame, ESC_KEY, curr_device_cnt
import cv2
import numpy as np
from save import VideoWriter_DEPTH,VideoWriter_RGB


if __name__ == "__main__":
    current_time = datetime.now().strftime('%Y%m%d_%H%M%S')
    output_path = f"./output/{current_time}/"
    if not os.path.exists(output_path):
        os.makedirs(output_path)
    rgb_video_writer1=VideoWriter_RGB(output_path + 'cam1/')
    rgb_video_writer2=VideoWriter_RGB(output_path + 'cam2/')
    rgb_video_writer3=VideoWriter_RGB(output_path + 'cam3/')

    depth_video_writer1=VideoWriter_DEPTH(output_path + 'cam1/')
    depth_video_writer2=VideoWriter_DEPTH(output_path + 'cam2/')
    depth_video_writer3=VideoWriter_DEPTH(output_path + 'cam3/')

    while True:
        try:
            color_images, depth_images = get_frame()
            if len(color_images) != curr_device_cnt or len(depth_images) != curr_device_cnt:
                continue

            rgb_video_writer1.write_one_frame(color_images[0])
            rgb_video_writer2.write_one_frame(color_images[1])
            # rgb_video_writer3.write_one_frame(color_images[2])
            depth_video_writer1.write_one_frame(depth_images[0])
            depth_video_writer2.write_one_frame(depth_images[1])
            # depth_video_writer3.write_one_frame(depth_images[2])

            depth_scaled_img1 = ((depth_images[0] - depth_images[0].min()) / (depth_images[0].max() - depth_images[0].min()) * 255).astype(np.uint8)
            depth_scaled_img2 = ((depth_images[1] - depth_images[1].min()) / (depth_images[1].max() - depth_images[1].min()) * 255).astype(np.uint8)
            depth_scaled_img3 = ((depth_images[1] - depth_images[1].min()) / (depth_images[1].max() - depth_images[1].min()) * 255).astype(np.uint8)

            # cv2.imshow("depth_data1 ", depth_scaled_img1)
            # cv2.imshow("depth_data2 ", depth_scaled_img2)
            # cv2.imshow("depth_data3 ", depth_scaled_img3)
            cv2.imshow("color_image1 ", color_images[0])
            cv2.imshow("color_image2 ", color_images[1])
            # cv2.imshow("color_image3 ", color_images[2])
            key = cv2.waitKey(33)
            if key == ord('q') or key == ESC_KEY:
                break
        except KeyboardInterrupt:
            break
    rgb_video_writer1.close_writer()
    rgb_video_writer2.close_writer()
    rgb_video_writer3.close_writer()
    depth_video_writer1.close_writer()
    depth_video_writer2.close_writer()
    depth_video_writer3.close_writer()
    cv2.destroyAllWindows()